<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller'
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import updateForm from './updateForm.vue';
  import userWiteList from './userWiteList.vue'
  import jackpotWhiteList from './jackpotWhiteList.vue';
  const updateFormRef = ref(),
  userWiteListRef = ref(),
  jackpotWhiteListRef = ref()
  const action = reactive({
    list:[] as Array<any>,
    SessionList:[] as Array<any>
  })
  const listQuery = reactive({
    game_id:'',
  })
  const typeChange = ()=>{
    getList()
  }
  const handleForm = (row:any = {})=>{
    updateFormRef.value.openDialog({
      ...row,
      SessionList:action.SessionList
    })
  }
  const handleDelete = (row:any = {})=>{
    const { id=0, item_name='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确认删除${item_name}白名单的配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      ChallengeApi.whiteDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getSession = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.gameSwitch()
    const { data=[] } = res
    action.SessionList = data
    loading.close()
    getList()
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.WhiteList(listQuery)
    const { data=[] } = res || {}
    action.list = data
    loading.close()
  }
  onMounted(() => {
    getSession()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">游戏类型：</div>
          <el-select v-model="listQuery.game_id" @change="typeChange" clearable placeholder="请选择类型">
            <el-option
              v-for="item in action.SessionList"
              :label="item.game_name"
              :value="item.game_id"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="typeChange">刷新</el-button>
          <el-button type="primary" @click="handleForm()">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="gameName" label="游戏类型" />
      <el-table-column min-width="120" prop="id" label="白名单ID" />
      <el-table-column min-width="120" prop="white_user" label="用户UUID">
        <template #default="scope">
          <el-tag
            v-for="item of scope.row.white_user"
            :key="item.uuid"
            style="cursor: pointer;">
              {{ item.uuid }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="120" prop="circulate" label="循环次数" />
      <el-table-column min-width="120" prop="content" label="描述" />
      <el-table-column width="200" fixed=right label="操作">
        <template #default="scope">
          <el-row>
            <el-col class="xm-mb10" :span="24">
              <el-button type="primary" @click="handleForm(scope.row)">编辑</el-button>
              <el-button type="primary" @click="jackpotWhiteListRef.openDialog(scope.row)">白名单配置</el-button>
            </el-col>
            <el-col :span="24">
              <el-button type="primary" @click="userWiteListRef.openDialog(scope.row)">用户配置</el-button>
              <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <updateForm ref="updateFormRef" @update-list="getList" />
    <userWiteList ref="userWiteListRef" />
    <jackpotWhiteList ref="jackpotWhiteListRef" />
  </div>
</template>

<style lang='scss' scoped>
  
</style>